/**   
 * Copyright © 2016 公司名. All rights reserved.
 * 
 * @Title: DictionaryServiceImpl.java 
 * @Prject: LianniuMgrSev
 * @Package: com.lianniu.mgr.baseService.dict.service
 * @Description: TODO

 * @date: 2016年3月31日 上午8:47:46 
 * @version: V1.0   
 */
package com.lianniu.mgr.baseService.dict.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.dict.dao.IDictionaryDao;
import com.lianniu.mgr.baseService.dict.model.DictionaryKeyValueVo;
import com.lianniu.mgr.baseService.dict.model.DictionaryVo;
import com.lianniu.mgr.baseService.dict.service.IDictionaryService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;

/** 
 * @ClassName: DictionaryServiceImpl 
 * @Description: TODO

 * @date: 2016年3月31日 上午8:47:46  
 */
@Service
public class DictionaryServiceImpl implements IDictionaryService {

  private final static Logger logger = Logger.getLogger(DictionaryServiceImpl.class);
  
  @Resource
  IDictionaryDao dictionaryDao;
  
  /* (non Javadoc) 
   * @Title: insertDictionary
   * @Description: TODO

   * @param curDictionaryVo
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#insertDictionary(com.lianniu.mgr.baseService.dict.model.DictionaryVo)
   * @date: 2016年3月31日 上午8:47:47 
   */
  @Override
  public BaseResult insertDictionary(DictionaryVo curDictionaryVo) {
    // TODO Auto-generated method stub
    logger.info("insertDictionary begin");
    logger.info("insertDictionary: input data check begin");
    logger.info("insertDictionary dictType:" +JSON.toJSONString(curDictionaryVo));
   
    
    BaseResult baseResult;
    if (StringUtil.isEmpty(curDictionaryVo.getId())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(curDictionaryVo.getTypeId())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(curDictionaryVo.getDicValue())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }  
    if (StringUtil.isEmpty(curDictionaryVo.getDicName())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }  
    
    logger.info("insertDictionary: input data check end");
    
    return BaseResult.success(dictionaryDao.insertDictionary(curDictionaryVo));
  }

  /* (non Javadoc) 
   * @Title: selectByDictName
   * @Description: TODO

   * @param curDictVo
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#selectByDictName(com.lianniu.mgr.baseService.dict.model.DictionaryVo)
   * @date: 2016年3月31日 上午8:47:47 
   */
  @Override
  public BaseResult selectByDictName(DictionaryVo curDictVo,PaginationInfo paginationInfo) {
    // TODO Auto-generated method stub
    logger.info("selectByDictName begin.");
    if (!StringUtil.isEmpty(curDictVo.getDicName())) {
      curDictVo.setDicName(curDictVo.getDicName().replace(" ", ""));
    }
    logger.info("selectByDictName dicName:" +curDictVo.getDicName());
    logger.info("selectByDictName paginationInfo:" +JSON.toJSONString(paginationInfo));
    
    curDictVo.setPaginationInfo(paginationInfo);
    List<DictionaryVo> list=dictionaryDao.selectByDictNameForPage(curDictVo);
    Map<String,Object> retmap=new HashMap<String,Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", list);
    logger.info("selectByDictName:  end");    
    
    return BaseResult.success(retmap); 
   
    
  }

  /* (non Javadoc) 
   * @Title: updateById
   * @Description: TODO

   * @param curDictVo
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#updateById(com.lianniu.mgr.baseService.dict.model.DictionaryVo)
   * @date: 2016年3月31日 上午8:47:47 
   */
  @Override
  public BaseResult updateById(DictionaryVo curDictVo) {
    // TODO Auto-generated method stub
    logger.info("updateById begin");
    logger.info("updateById: update data check begin");
    logger.info("updateById curDictType:" +JSON.toJSONString(curDictVo));
  
    
    BaseResult baseResult;
    if (StringUtil.isEmpty(curDictVo.getId())) {
      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(curDictVo.getDicName())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(curDictVo.getDicValue())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    
    logger.info("updateById: update data check end");
    
    return BaseResult.success(dictionaryDao.updateById(curDictVo));
  }

  /* (non Javadoc) 
   * @Title: deleteById
   * @Description: TODO

   * @param Id
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#deleteById(java.lang.String)
   * @date: 2016年3月31日 上午8:47:47 
   */
  @Override
  public BaseResult deleteById(String Id) {
    // TODO Auto-generated method stub    
    logger.info("deleteById begin");
    logger.info("deleteById Id:" +Id);
  
    return BaseResult.success(dictionaryDao.deleteById(Id));
    
  }

  /* (non Javadoc) 
   * @Title: selectById
   * @Description: TODO

   * @param id
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#selectById(java.lang.String)
   * @date: 2016年3月31日 上午8:47:47 
   */
  @Override
  public BaseResult selectById(DictionaryVo curDictVo) {
    // TODO Auto-generated method stub
    logger.info("selectById begin");
    logger.info("selectById Id:" +curDictVo.getId());
  
    return BaseResult.success(dictionaryDao.selectById(curDictVo.getId()));
  }

  /* (non Javadoc) 
   * @Title: deleteDictionaryBatch
   * @Description: TODO

   * @param id
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#deleteDictionaryBatch(java.lang.String[])
   * @date: 2016年4月2日 下午10:32:39 
   */
  @Override
  public BaseResult deleteDictionaryBatch(String... id) {
    // TODO Auto-generated method stub
    logger.info("deleteDictionaryBatch begin");
    logger.info("deleteDictionaryBatch Ids:" +id.toString());
    logger.info("deleteDictionaryBatch: input data check begin");
   
    BaseResult baseResult;
    
    if (StringUtil.isEmpty(id.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("deleteDictionaryBatch: input data check end");
    
    return BaseResult.success(dictionaryDao.deleteDictionaryBatch(id));
  }

  /* (non Javadoc) 
   * @Title: checkDicName
   * @Description: TODO

   * @param curDictVo
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#checkDicName(com.lianniu.mgr.baseService.dict.model.DictionaryVo)
   * @date: 2016年4月7日 上午8:39:35 
   */
  @Override
  public Boolean checkDicName(DictionaryVo curDictVo) {
    // TODO Auto-generated method stub
    logger.info("checkDicName begin");
    logger.info("checkDicName typeName:" +JSON.toJSONString(curDictVo));
    logger.info("checkDicName: input data check begin");    
    
    if (StringUtil.isEmpty(curDictVo.getTypeId())) {     
      return false;
    }
    if (StringUtil.isEmpty(curDictVo.getDicName())) {     
      return false;
    }
    logger.info("checkDicName: input data check end");    
   
    int count=dictionaryDao.countDicName(curDictVo);
    if(count>0){
      return false;      
    }
    else{
      return true;
    } 
  }

  /* (non Javadoc) 
   * @Title: checkDicValue
   * @Description: TODO

   * @param curDictVo
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#checkDicValue(com.lianniu.mgr.baseService.dict.model.DictionaryVo)
   * @date: 2016年4月7日 上午8:39:35 
   */
  @Override
  public Boolean checkDicValue(DictionaryVo curDictVo) {
    // TODO Auto-generated method stub
    logger.info("checkDicValue begin");
    logger.info("checkDicValue typeName:" +JSON.toJSONString(curDictVo));
    logger.info("checkDicValue: input data check begin");   
  
    if (StringUtil.isEmpty(curDictVo.getTypeId())) {     
      return false;
    }
    if (StringUtil.isEmpty(curDictVo.getDicValue())) {      
      return false;
    }
    logger.info("checkDicValue: input data check end");    
   
    int count=dictionaryDao.countDicValue(curDictVo);
    if(count>0){
      return false;      
    }
    else{
      return true;
    }
  }

  /* (non Javadoc) 
   * @Title: selectDictionaryByDicType
   * @Description: TODO

   * @param curDictVo
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#selectDictionaryByDicType(com.lianniu.mgr.baseService.dict.model.DictionaryVo)
   * @date: 2016年4月8日 下午3:45:45 
   */
  @Override
  public BaseResult selectDictionaryByDicType(DictionaryVo curDictVo) {
    // TODO Auto-generated method stub
    logger.info("selectDictionaryByDicType begin.");
    
    BaseResult baseResult;
    if (StringUtil.isEmpty(curDictVo.getTypeName())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }   
    
    logger.info("selectDictionaryByDicType typeName:" +curDictVo.getTypeName());      
    logger.info("selectDictionaryByDicType data check end");    
   
    List<DictionaryKeyValueVo> rtmap=dictionaryDao.selectDictionaryByDicType(curDictVo);
   
    return BaseResult.success(rtmap); 
  }

  /* (non Javadoc) 
   * @Title: delBatchDictTypeLogic
   * @Description: TODO

   * @param updateUser
   * @param id
   * @return 
   * @see com.lianniu.mgr.baseService.dict.service.IDictionaryService#delBatchDictTypeLogic(java.lang.String, java.lang.String[])
   * @date: 2016年4月9日 下午7:38:51 
   */
  @Override
  public BaseResult delBatchDictionaryLogic(String updateUser, String... id) {
    // TODO Auto-generated method stub
    logger.info("delBatchDictTypeLogic begin");
    logger.info("delBatchDictTypeLogic Ids:" +id.toString());
    logger.info("delBatchDictTypeLogic: input data check begin");
   
    BaseResult baseResult;
    if (StringUtil.isEmpty(id.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("delBatchDictTypeLogic: input data check end");
    
    return BaseResult.success(dictionaryDao.delBatchDictionaryLogic(updateUser, id));
  }

  /**
   * @Description:根据字典类型和字典值查询字典名
   * @param dictTypeName
   * @param dictVal
   * @return String

   * @Date:2016年4月11日下午5:09:02
   * @Version 1.0.0
   */
  public BaseResult getDictNameByDictVal(String dictTypeName,String dictVal){
    if(StringUtils.isEmpty(dictTypeName) || StringUtils.isEmpty(dictVal)){
      return BaseResult.success();
    }
    logger.info(String.format("根据字典类型和字典值查询字典名，type=[%s],val=[%s]", dictTypeName,dictVal));
    String val = dictionaryDao.getDictNameByDictVal(dictTypeName, dictVal);
    logger.info("value is : "+ val);
    return BaseResult.success(val);
  }
}
